//
//  ProductController.m
//  FactorySale
//
//  Created by 胡小康 on 2020/4/21.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "ProductController.h"
#import "ProductCell.h"
#import "CooperationPriductListModel.h"
#import "ChangePricePopViewController.h"
#import "CooperationPriductListModel.h"

@interface ProductController ()<ProductCellDelegate>
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *listArray;
@end

@implementation ProductController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
- (void)setupUI{
    self.title = @"合作产品";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    
    self.titleLabel.text = self.model.parts_name;
    self.listArray = [CooperationPriductListModel mj_objectArrayWithKeyValuesArray:self.model.attrubites];
    
    self.tableView.rowHeight = 110;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView reloadData];
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ProductCell *cell = [ProductCell cellWithTableView:tableView];
    [cell assignment:self.listArray[indexPath.row] withUnit:self.model.unit];
  //cell.model = self.listArray[indexPath.row];
    cell.indexPath = indexPath;
    cell.delegate = self;
//    cell.productCellBlock = ^{
//        ChangePricePopViewController *changePriceVC = [ChangePricePopViewController new];
//        [_weakSelf.navigationController presentViewController:changePriceVC animated:YES completion:nil];
//    };
    return cell;
}
#pragma mark - ProductCellDelegate
- (void)productCellChangePriceClick:(NSIndexPath *)indexPath {
    __weak typeof(self) _weakSelf = self;
    CooperationPriductListModel *model = self.listArray[indexPath.row];
    ChangePricePopViewController *changePriceVC = [ChangePricePopViewController new];
    changePriceVC.model = model;
    changePriceVC.confirmBtnClickBlock = ^(NSString * _Nonnull contractPrice, NSString * _Nonnull price) {
        [_weakSelf submmitChangePrice:contractPrice price:price attrID:model.id];
    };
    [_weakSelf.navigationController presentViewController:changePriceVC animated:YES completion:nil];
}
#pragma mark - 网络请求
- (void)submmitChangePrice:(NSString *)contractPrice price:(NSString *)price attrID:(NSString *)attrID {
    __weak typeof(self) _weakSelf = self;
    [MBProgressHUD showHUDAddedTo:self.view msg:@"修改中..." animated:YES];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"contractPrice"] = contractPrice;
    param[@"salePrice"] = price;
    param[@"id"] = attrID;
    [NetWorkTool POST:@"/cs_platform/partsApi/updatePartsPrice" param:param success:^(id dic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"%@", dic);
        ShowToastWithText(dic[@"msg"]);
        [_weakSelf getProductListData];
    } other:^(id dic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:YES];
}
/**
 获取商品列表数据
 */
- (void)getProductListData {
    __weak typeof(self) _weakSelf = self;
    [self.listArray removeAllObjects];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"partsId"] = self.model.id;
    [NetWorkTool POST:@"/cs_user/getFactoryPartsAttribute" param:param success:^(id dic) {
        NSLog(@"%@", dic);
        NSArray *tempArray = [CooperationPriductListModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        [_weakSelf.listArray addObjectsFromArray:tempArray];
        [_weakSelf.tableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
    } needUser:YES];
    
}
@end
